package com.atguigu.yygh.hosp.testmongo;

import com.atguigu.yygh.common.R;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/mongo")
public class TestMongo1 {
    @Autowired
    private MongoTemplate mongoTemplate;

    @GetMapping("create")
    public void createUser() {
        User user = new User();
        user.setAge(22);
        user.setName("jack");
        user.setEmail("jack@163.com");
        mongoTemplate.insert(user);
        System.out.println("user = " + user);
    }

    //查询所有
    @GetMapping("findAll")
    public void findUser() {
        List<User> list = mongoTemplate.findAll(User.class);
        list.forEach(System.out::println);
    }

    @GetMapping("findId")
    public void getById() {
        User user = mongoTemplate.findById("63fc712abc5c7a08baf827d2", User.class);
        System.out.println("user = " + user);
    }

    @GetMapping("findUser")
    public void findUserList() {
        Query query = new Query(Criteria.where("name").is("tom").and("age").is(20));

        List<User> userList = mongoTemplate.find(query, User.class);
        userList.forEach(System.out::println);
    }

    //模糊查询
    @GetMapping("findLike")
    public void findUsersLikeName() {
        String name = "om";
        String regex = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern pattren = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);

        Query query = new Query(Criteria.where("name").regex(pattren));

        List<User> userList = mongoTemplate.find(query, User.class);
        userList.forEach(System.out::println);
    }

    //分页查询
    @GetMapping("findPage")
    public void findUsersPage() {

        String name = "om";
        Integer pageNo = 1;
        Integer pageSize = 10;
        String regex = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern pattren = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);

        Query query = new Query();
        query.addCriteria(Criteria.where("name").regex(pattren));
        long count = mongoTemplate.count(query, User.class);

        List<User> userList = mongoTemplate.find(query.skip((pageNo - 1) * pageSize).limit(pageSize), User.class);
        HashMap<Object, Object> pageMap = new HashMap<>();
        pageMap.put("list", userList);
        pageMap.put("count", count);
        System.out.println("pageMap = " + pageMap);

    }

    @GetMapping("update")
    public void updateUser() {
        User user = mongoTemplate.findById("63fc712abc5c7a08baf827d2", User.class);
        user.setName("rose");
        user.setEmail("rose@163.com");
        Query query = new Query(Criteria.where("_id").is(user.getId()));
        Update update = new Update();
        update.set("name", user.getName());
        update.set("age", user.getAge());
        update.set("email", user.getEmail());
        UpdateResult result = mongoTemplate.upsert(query, update, User.class);
        long count = result.getModifiedCount();
        System.out.println(count);
    }

    @GetMapping("delete")
    public void delete() {
        Query query =
                new Query(Criteria.where("_id").is("5ffbfa2ac290f356edf9b5aa"));
        DeleteResult result = mongoTemplate.remove(query, User.class);
        long count = result.getDeletedCount();
        System.out.println(count);
    }
}
